/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.service.base.test.convert;

import cn.hutool.core.collection.ListUtil;
import org.junit.jupiter.params.converter.ArgumentConversionException;
import org.junit.jupiter.params.converter.SimpleArgumentConverter;

import java.util.List;

/**
 * <b>(StringArrayConverter)</b>
 *
 * @author LiDaDaHai
 * @version 1.0.0
 * @since 2022/7/29
 */
@SuppressWarnings("unused")
public class StringListConverter extends SimpleArgumentConverter {

    @Override
    protected Object convert(Object source, Class<?> targetType) throws ArgumentConversionException {
        if (source instanceof String && List.class.isAssignableFrom(targetType)) {
            String[] split = ((String) source).split("\\s*,\\s*");
            return ListUtil.toList(split);
        } else {
            throw new IllegalArgumentException("Conversion from " + source.getClass() + " to "
                    + targetType + " not supported.");
        }
    }

}